import sys
sys.path.append('..\model')
import city_model
import requests
class City_fileController(object):
    def __init__(self):
        self.city_model = city_model.CityModel()
    #获取城市
    def index(self):
        with open('../file/china-city-list.txt','r',encoding='UTF-8') as files:
            for line in files.readlines():
                line_obj=line.split()
                exists_data = self.city_model.isCity_exists(line_obj[2])
                if (exists_data == 0):
                    id=line_obj[0]
                    name=line_obj[2]
                    province=line_obj[7]
                    superior=line_obj[9]
                    latitude=float(line_obj[10])
                    longitude=float(line_obj[11])
                    self.city_model.add_city(id,name,province,superior,latitude,longitude)
    #获取景点
    def scenic(self):
        with open('../file/china-scenic-list.txt','r',encoding='UTF-8') as files:
            for line in files.readlines():
                line_obj=line.split()
                self.city_model.add_scenic(line_obj[1],line_obj[2])
                print(line_obj[1])
    #气象状态码文件处理
    def weather_code(self):
        with open('../file/condition-code.txt','r',encoding='UTF-8') as files:
            for line in files:
                line_obj=line.split()
                #下载图片并保存
                img_file=requests.get(line_obj[-1])
                img_name=line_obj[0]+'.png'
                open('../cond_icon/'+img_name, 'wb').write(img_file.content)
                #添加数据库
                code=int(line_obj[0])
                self.city_model.add_weather_code(code,line_obj[1],img_name)
                print(line_obj[0])

if __name__=='__main__':
    obj=City_fileController()
    #obj.index()
    obj.scenic()
    #obj.weather_code()